﻿using System;
using System.Web.UI.WebControls;
using TravelAgency.DAL;

namespace TravelAgency.AspNet.Agents
{
    public partial class CreateTrip : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }        

        protected void LinkButtonCreate_Click(object sender, EventArgs e)
        {
            this.Validate();

            if (Page.IsValid)
            {
                TravelAgencyDAO dao = new TravelAgencyDAO();
                using (dao)
                {
                    try
                    {
                        Trip newTrip = new Trip();
                        newTrip.Title = this.LabelTitle.Text;
                        newTrip.Description = this.LabelDescription.Text;
                        newTrip.MaxPeopleCapacity = int.Parse(this.LabelMaxPeopleCount.Text);
                        newTrip.Price = (decimal)double.Parse(this.LabelPrice.Text);
                        newTrip.StartDate = (DateTime)this.RadDatePickerStartDate.SelectedDate;
                        newTrip.EndDate = (DateTime)this.RadDatePickerEndDate.SelectedDate;

                        byte[] fileBytes;
                        if (this.TryGetFileBytes(this.FileUploadPhoto, out fileBytes))
                        {
                            Photo photo = dao.AddPhoto(this.FileUploadPhoto.FileName, fileBytes);
                            dao.SaveChanges();
                            newTrip.PhotoId = photo.PhotoId;
                        }
                        else
                        {
                            newTrip.PhotoId = null;
                        }

                        dao.Add(newTrip);
                        dao.SaveChanges();
                        this.Response.Redirect("~/Agents/TripCreated.aspx");
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
        }

        protected void LinkButtonCancel_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("~/Agents/CreateTripCanceled.aspx");
        }

        private bool TryGetFileBytes(FileUpload fileUpload, out byte[] fileBytes)
        {
            if (fileUpload.HasFile)
            {
                fileBytes = fileUpload.FileBytes;
                return true;
            }
            else
            {
                fileBytes = null;
                return false;
            }            
        }

        protected void CustomValidatorStartDate_ServerValidate(object source, ServerValidateEventArgs args)
        {
            this.ValidateDate(args);
        }

        protected void CustomValidatorEndDate_ServerValidate(object source, ServerValidateEventArgs args)
        {
            this.ValidateDate(args);
        }

        private void ValidateDate(ServerValidateEventArgs args)
        {
            if (this.RadDatePickerStartDate.SelectedDate == null ||
                this.RadDatePickerEndDate.SelectedDate == null)
            {
                args.IsValid = false;
            }

            bool isDateInFuture = (DateTime)this.RadDatePickerStartDate.SelectedDate > DateTime.Now;
            bool isControlValid =
                this.RadDatePickerStartDate.SelectedDate <= this.RadDatePickerEndDate.SelectedDate && isDateInFuture;

            args.IsValid = isControlValid;
        }
    }
}